import request from '@/utils/request'

export function getVersionList() {
  return request({
    url: '/question/version/list',
    method: 'GET'
  })
}

export function exportItem(params) {
  const p = serialize(params)
  return request({
    url: `/question/statistics/tm/detail?${p}`,
    method: 'GET',
    responseType: 'arraybuffer'
  })
}

export function getUserDetail(params) {
  const p = serialize(params)
  return request({
    url: `/question/detail?${p}`,
    method: 'GET'
  })
}

export function questionRandomCodeInsert(params) {
  return request({
    url: `/question/random/code/insert`,
    method: 'POST',
    data: params
  })
}
export function questionRandomCode(params) {
  const p = serialize(params)
  return request({
    url: `/question/random/code/execlExport?${p}`,
    responseType: 'arraybuffer',
    method: 'GET'
  })
}

export function initData(url, params) {
  return request({
    url: url,
    method: 'get',
    params
  })
}

/**
 * json对象转url参数
 * @param {*} obj
 */
function serialize(obj) {
  var ary = []
  for (var p in obj) {
    if (obj.hasOwnProperty(p) && obj[p]) {
      ary.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]))
    }
  }
  return ary.join('&')
}
